<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
	<head>
		<meta charset="UTF-8">
		<title>后台管理页面</title>
		<link rel="stylesheet" type="text/css" th:href="@{/css/cart.css}" />
		<link rel="stylesheet" type="text/css" th:href="@{/dist/css/bootstrap.min.css}">
		<link rel="stylesheet" type="text/css" th:href="@{/css/index.css}"/>
		<link rel="stylesheet" type="text/css" th:href="@{/css/animate.css}"/>
		<link rel="stylesheet" th:href="@{/layui/css/layui.css}">

	</head>

	<body>
	<!--导航部分  begin-->
	<div th:replace="admin/common/header :: nav"></div>
	<!--导航部分 end-->

		<!--购物车 begin-->
		<!--外层div-->

		<div id="allBooksData"></div>
		<div class="container">
			<!--首页底部信息 begin-->
			<div th:replace="common/footer :: footer"></div>
		</div>
		<!--end-->

		<!--引入js文件-->
		<script src="js/details.js" th:src="@{/js/details.js}" type="text/javascript" charset="utf-8"></script>
		<script src="js/cart.js" th:src="@{/js/cart.js}" type="text/javascript" charset="utf-8"></script>
		<script src="dist/js/jquery.min.js" th:src="@{/dist/js/jquery.min.js}"></script>
		<script src="js/carousel.js" th:src="@{/js/carousel.js}" type="text/javascript" charset="utf-8"></script>
		<script src="dist/js/bootstrap.min.js" th:src="@{/dist/js/bootstrap.min.js}"></script>
		<script src="js/wow.js" th:src="@{/js/wow.js}"></script>
		<script th:src="@{/layui/layui.js}"></script>
		<script th:inline="javascript">
			//获取应用路径
			var contextPath = [[${#request.getContextPath()}]];
			var pages = [[${pages}]];
			var admin = [[${session.admin}]];
			var category = [[${category}]];
		</script>
		<script type="text/javascript">
			if(!(/msie [6|7|8|9]/i.test(navigator.userAgent))) {
				new WOW().init();
			};
			//根据图书名搜索
			function searchBookName() {
				var inputBookName = $("#inputBookName").val();
				if (inputBookName != ''){
					window.location.href = contextPath + "/admin/searchBook?inputBookName=" + inputBookName;
				}else alert('查找内容不为空')
			}


			//预览图书简介
			function displayInfo(book,date) {
				layer.open({
					title: '书本详情'
					,content:
					'ISBN：'+book.isbn+'<br>'
					+'书名：'+book.name+'<br>'
					+'作者：'+book.author+'<br>'
					+'出版社：'+book.publisher+'<br>'
					+'出版日期：'+date+'<br>'
					+'分类：'+book.cate+'<br>'
					+'库存：'+book.count+'<br>'
					+'原价：'+book.oldPrice+'<br>'
					+'现价：'+book.newPrice+'<br>'
					+'是否推荐：'+ (book.recommend==0?'否':'是')+'<br>'
					+'上架状态：'+ (book.state==0?'下架中':'上架中')+'<br>'
					+'简介：'+book.info+'<br>'
					,area: '500px'
				});

			}

			//删除图书
			function deleteBook(bookId){
				layer.confirm('确定删除？', {
					icon:3,
					btn: ['确定', '取消']
				}, function(index, layero){
					//按钮【按钮一】的回调
					$.ajax({
						url: contextPath + "/admin/deleteBook",
						method: "post",
						data: {"bookId":bookId},
						success: function (data) {
							if (data == 'success') {
								window.location.href = contextPath + "/admin/toBookAdmin"
							} else layer.msg('修改失败')
						}
					})
				}, function(index){
					return ;
				});
			}
		</script>

	<style type="text/css">
		td {text-align: center}
		th {text-align: center}
	</style>
	</body>
	<script>
		//分页js
		//页面加载时执行
		var ids ='';
		var bookIds = new Set();
		$(function () {
			if (admin == null||admin==undefined||admin == 'undefined'){
				location.href =contextPath+"/book/index";
			}
			$("#allBooksData").load(contextPath + "/admin/getBookListByPage",queryData(1,'',category));
/*			if (sessionStorage.getItem("ids") != null){
				alert(sessionStorage.getItem("ids") );
			}*/
			//存储购物记录的id
/*			var ids = sessionStorage.getItem("ids");
			// var thirdModel = document.getElementById("third_del");
			//判断是否选择了商品
			var count = 0;
			//获取所有的checkbox
			var cks = document.getElementsByName("cks");
			//遍历
			for(var i = 0; i < cks.length; i++) {
				if(cks[i].checked) {
					count++;
					ids += cks[i].value + ",";
				}
			}
			sessionStorage.setItem("ids",ids);*/
			for (var x of bookIds) { // 遍历Set
				// alert(x);
				document.getElementsByName("cks").checked = true;
			}

		})


		//跳转到指定页
		function goPage(pageSize,pages) {
			var inputPage = $("#inputPage").val();
			//数字正则
			var reg = new RegExp("^[0-9]*$");
			if (!reg.test(inputPage)){
				layer.msg('请输入正整数！', {icon: 7,anim:6});
				return;
			}
			if (inputPage==''){
				layer.msg('请输入页码', {icon: 7,anim:6});
				return;
			}
			if (inputPage > pages) {//如果输入的页数大于总页数
				layer.msg('超出总页数！总页数：'+pages, {icon: 7,anim:6});
				return;
			}
			$("#allBooksData").load(contextPath + "/admin/getBookListByPage",queryData(inputPage,pageSize,category))

		}

		//修改指定每页显示的记录数
		function loadDataBySize(obj) {
			$("#allBooksData").load(contextPath + "/admin/getBookListByPage",queryData(1,obj.value,category))
		}
		//查询
		function queryData(page, pageSize,category) {
			var query = {};
			query.page = page;
			query.pageSize = pageSize == '' ? 4 : pageSize;
			query.category = category == undefined ? '': category;
			return query;

		}
		function cks(obj) {
	}
	function loadData(page,pageSize) {
		$("#allBooksData").load(contextPath + "/admin/getBookListByPage",queryData(page,pageSize,category));
/*		for (var i = 0;i<bookIds.size;i++){
			document.getElementsByName("cks")[i].value.checked=true;
			alert(bookIds.size)
		}*/

	}

		function deleteBooks(){
			//存储购物记录的id
			var ids = '';
			var thirdModel = document.getElementById("third_del");
			//判断是否选择了商品
			var count = 0;
			//获取所有的checkbox
			var cks = document.getElementsByName("cks");
			//遍历
			for(var i = 0; i < cks.length; i++) {
				if(cks[i].checked) {
					count++;
					ids += cks[i].value + ",";
					bookIds.add(cks[i].value);
				}
			}
			// var s = new Array();
			for(var x of bookIds){
				console.log(x)
			}

			if (ids==''){
				layer.msg('请勾选图书！');
				return;
			}
			layer.confirm('确定删除？', {
				icon:3,
				btn: ['确定', '取消']
			}, function(index, layero){
				//按钮【按钮一】的回调
				$.ajax({
					url:contextPath + "/admin/deleteBooks",
					data:{"ids":ids},
					type:"post",
					success:function (data) {
						if (data=="success"){
							layer.msg('删除成功', {
								icon: 1,
								time: 2000 //2秒关闭（如果不配置，默认是3秒）
							}, function(){
								//do something
								location.href = contextPath +"/admin/toBookAdmin"
							});
						}
						else layer.msg("删除失败")

					}
				})
			}, function(index){
				return ;
			});

	}

		function operate(obj,bookId) {
			if (obj.value=='update'){
				location.href = contextPath + "/admin/toUpdateBook?bookId="+bookId;
			}else if (obj.value == 'offShelf'){

				layer.confirm('确定下架？', {
					icon:3,
					btn: ['确定', '取消']
				}, function(index, layero){
					//按钮【按钮一】的回调
					location.href = contextPath + "/admin/offShelf?bookId="+bookId;
				}, function(index){
					return ;
				});
			}else if (obj.value == 'onShelf'){
				layer.confirm('确定上架？', {
					icon:3,
					btn: ['确定', '取消']
				}, function(index, layero){
					//按钮【按钮一】的回调
					location.href = contextPath + "/admin/onShelf?bookId="+bookId;
				}, function(index){
					return ;
				});
			} else if (obj.value == 'addCount'){
				layer.prompt({
					formType: 2,
					value: '10',
					title: '请输入补充量',
					area: ['200px', '20px'] //自定义文本域宽高
				}, function(value, index, elem){
					var reg = new RegExp("^\\+?[1-9][0-9]*$");//正整数
					if (!reg.test(value)){
						layer.msg('请输入正整数！', {icon: 7,anim:6});
						return;
					}else location.href = contextPath + "/admin/addCount?bookId="+bookId+"&count="+value;
				});
			} else if (obj.value == 'changePrice'){
				layer.prompt({
					formType: 2,
					value: '10',
					title: '请输入新价格',
					area: ['200px', '20px'] //自定义文本域宽高
				}, function(value, index, elem){
					var reg = new RegExp("^\\d+(\\.\\d+)?$");//非负浮点数>=0
					if (!reg.test(value)){
						layer.msg('请输入非负浮点数！', {icon: 7,anim:6});
						return;
					}else location.href = contextPath + "/admin/changePrice?bookId="+bookId+"&price="+value;
				});
			} else if(obj.value == 'setRec'){
				layer.confirm('确定设为推荐？', {
					icon:3,
					btn: ['确定', '取消']
				}, function(index, layero){
					//按钮【按钮一】的回调
					location.href = contextPath + "/admin/setRecBook?bookId="+bookId;
				}, function(index){
					return ;
				});
			}else if(obj.value == 'setNotRec'){
				layer.confirm('确定设为非推荐？', {
					icon:3,
					btn: ['确定', '取消']
				}, function(index, layero){
					//按钮【按钮一】的回调
					location.href = contextPath + "/admin/setNotRecBook?bookId="+bookId;
				}, function(index){
					return ;
				});
			}

		}
	</script>
<!--					$.ajax({
						url:contextPath + "/admin/setRecBook",
						data:{"bookId":bookId},
						type:"post",
						success:function (data) {
							if (data=="success"){
								layer.msg('设置成功', {
									icon: 1,
									time: 2000 //2秒关闭（如果不配置，默认是3秒）
								}, function(){
									//do something
									location.href = contextPath +"/admin/toBookAdmin"
								});
							} else if (data == "moreThan3"){
								layer.msg('推荐数量不能大于3！', {icon: 7,anim:6});
							}
							else layer.msg("删除失败")

						}
					})-->
</html>